package com.koron.order.defectPending.controller;

import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.defectPending.bean.po.DefectPendingPo;
import com.koron.order.defectPending.bean.po.DefectPendingStatus;
import com.koron.order.defectPending.service.DefectPendingService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DefectPendingReviewController
 * @description
 * @Author zhouj
 * @Date 2021/9/24 11:42
 */
@Slf4j
@RequestMapping("/defectPending/waitReview/")
@RestController
@Api(tags = "缺陷工单-待处理")
public class DefectPendingReviewController implements WorkflowController {

    @Autowired
    DefectPendingService defectPendingService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 修改业务状态
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核提交:{}", redirectQuery.toString());
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            //修改工单状态为草稿
            modify(redirectQuery, DefectPendingStatus.DRAFT.getStatus());
        } else {
            modify(redirectQuery, DefectPendingStatus.WAIT_REVIEW.getStatus());
        }
        return Response.ok();
    }

    /**
     * 结束时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核结束:{}", redirectQuery.toString());
        ADOConnection.runTask(getCurrDs(), defectPendingService, "submitDefectPending", Integer.class, redirectQuery.getDataId(), DefectPendingStatus.REVIEWED.getStatus());
        return Response.ok();
    }

    /**
     * 作废时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        //作废时,只将工单状态变为已废弃即可
        log.info("工单审核作废:{}", redirectQuery.toString());
        if (redirectQuery.getOperation() == 16) {
            //说明作废
            //修改工单状态为已废弃
            modify(redirectQuery, DefectPendingStatus.DEPRECATED.getStatus());
        }
        return Response.ok();
    }

    public void modify(RedirectQuery redirectQuery, String status) {
        //修改工单状态为草稿
        DefectPendingPo defectPendingPo = new DefectPendingPo();
        defectPendingPo.setId(redirectQuery.getDataId());
        defectPendingPo.setStatus(status);
        ADOConnection.runTask(getCurrDs(), defectPendingService, "update", Integer.class, defectPendingPo);
    }

}
